package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.DoctorInfoAdapter;
import com.anbinh.doctor.adapter.SpecialAdapter;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ItemSpecial;
import com.anbinh.doctor.model.ResponseSpecial;
import com.anbinh.doctor.model.ResponseSpecial.Child;
import com.anbinh.doctor.model.ResponseSpecial.Item;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAddChuyenkhoa extends SherlockFragment {
	private static FragmentAddChuyenkhoa fragment = null;
	private View mView;
	private SpecialAdapter adapter;
	private ListView mGridView;
	private CircleProgressBar loadding;
	private ItemSpecial currentDoctorInfo;
	private List<Item> mListSp;
	private static int mDoctorId;

	public static FragmentAddChuyenkhoa newInstance(Bundle bundle) {
		if (fragment == null)
			fragment = new FragmentAddChuyenkhoa();
		mDoctorId = bundle.getInt(Utils.KEY_DOCTORID);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0, R.id.dia_diem_kham, Menu.NONE, "Hoàn thành")
				.setIcon(R.drawable.ic_done)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		pushSpecialList();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		super.onResume();
		((DetailtActivity) getActivity()).setActionBarTitle(getResources()
				.getString(R.string.them_chuyen_khoa));

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_thongtincanhan, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		adapter = new SpecialAdapter(getActivity(), 0, null);

		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ItemSpecial item = adapter.getItem(position);
				if (adapter.getItemViewType(position) == DoctorInfoAdapter.TYPE_SEPARATOR) {
					Log.d("onItemClick", "onItemClick");
					adapter.onCheck(item.getIdGroupName(), !item.getItem()
							.getItem().getChecked());
				} else {
					adapter.onCheckItem(position, !item.getChild().getItem()
							.getChecked());
				}
			}

		});
		onLoadData();
	}

	public void pushSpecialList() {
		String list = getListSpecial();
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity()).requestPushSpecialList(
				token, list, new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						Utils.showMessage(getActivity(), data.getDescription()
								+ " " + data.getResponsecode());
						Intent intent = new Intent();
						intent.putExtra(Utils.KEY, Utils.RESUME_EXTRA_INFO);
						getActivity().setResult(Integer.MAX_VALUE, intent);
						getActivity().finish();
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}

				});
	}

	private String getListSpecial() {
		String result = "";

		for (int i = 0; i < adapter.getmData().size(); i++) {
			ItemSpecial itemSp = adapter.getmData().get(i);
			Item item = itemSp.getItem();
			if (item != null && item.getItem().getChecked()) {
				result = item.getItem().getId() + "," + result;
			}
			if (itemSp.getChild() != null
					&& itemSp.getChild().getItem().getChecked()) {
				result = itemSp.getChild().getItem().getId() + "," + result;
			}
		}
		return result.substring(0, result.length() - 1);
	}

	private void onLoadData() {
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity()).requestGetSpecial(token,
				mDoctorId, new OnRequest<ResponseSpecial>() {

					@Override
					public void onSuccess(ResponseSpecial data) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						if (data.getResponsecode() >= 0) {
							mListSp = data.getData().getItems();
							for (int i = 0; i < mListSp.size(); i++) {
								ItemSpecial itemSp = new ItemSpecial();
								Item item = mListSp.get(i);
								itemSp.setItem(item);
								itemSp.setId(item.getItem().getId());
								itemSp.setIdGroupName(item.getItem().getId());
								adapter.addSeparatorItem(itemSp);
								if (item.getChildren() != null
										&& item.getChildren().size() > 0) {
									for (int j = 0; j < item.getChildren()
											.size(); j++) {
										ItemSpecial itemSp_ = new ItemSpecial();
										itemSp.setIdGroupName(item.getItem()
												.getId());
										Child child = item.getChildren().get(j);
										itemSp_.setChild(child);
										itemSp_.setId(child.getItem().getId());
										adapter.addItem(itemSp_);
									}

								}

							}
							mGridView.setAdapter(adapter);
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
					}
				});
	}

	public ItemSpecial getCurrentDoctorInfo() {
		return currentDoctorInfo;
	}

}
